var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var payUtil = require('../../../utils/pay.js');
Page({
  data: {
    orderId: null,
    type: "",
    orderDetail: {},
    winHeight:0

  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderId: options.id,
      type: options.type
    });
    this.getOrderDetail();
    var that=this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winHeight: res.windowHeight
        })
      },
    })
  },
  getOrderDetail() {
    let that = this;
    util.request(api.OrderDetail, { id: that.data.orderId, type: that.data.type }).then(function (res) {
      if (res.code === '200') {
        console.log(res.data);
        var orderDetail = res.datas;
        if (that.data.type == 'hotel') {
          var checkOutTime = new Date(orderDetail.check_out_time);
          orderDetail['outTime'] = checkOutTime.getFullYear() + "-" + (checkOutTime.getMonth() + 1) + "-" + checkOutTime.getDate();
        }
        that.setData({
          orderDetail: res.datas
        });
        //that.payTimer();
      }
    });
  },
  payTimer() {
    let that = this;
    let orderInfo = that.data.orderInfo;

    setInterval(() => {
      console.log(orderInfo);
      orderInfo.add_time -= 1;
      that.setData({
        orderInfo: orderInfo,
      });
    }, 1000);
  },
  payOrder(e) {
    var orderId = e.currentTarget.dataset.id;
    payUtil.pay(orderId, this.data.type)

  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  cancelOrder: function (e) {
  wx.showLoading({
    title: '请稍后',
  })
    let that = this;
    var orderId = e.currentTarget.dataset.id;
    var type = that.data.type;
    var data = {
      id: orderId,
      type: that.data.type,
      token: wx.getStorageSync("token")
    }
    util.request(api.OrderCancel, data).then(function (res) {
      wx.hideLoading();
      if (res.code == "200") {
        //刷新列表
        wx.showToast({
          title: '取消成功',
        })
        wx.navigateBack({
          
        })
      }else{
        wx.showToast({
          title: res.message,
        })
      }
    })
  }
})